<template>
	<view>
		<view class="content">
			<view class="logo">
				<u-avatar :src="avatarUrl" size="250" mode="square" class="avatar"></u-avatar>
				<br><br>
				<view class="logoText">更新密码</view>
			</view> 
			<view>
				<u-form :model="form" ref="uForm">
					<!-- <u-form-item label="学号" prop="no"> -->
					<u-form-item label="学号">
						{{no}}
					</u-form-item>
					<u-form-item label="密码" prop="password" >
						<u-input v-model="form.password" type="password" placeholder="请输入最新密码"/>
					</u-form-item>
					<u-form-item label="验证码" prop="captcha" v-if="isVerification">
						<view class="flex">
							<view class="input"><u-input v-model="form.captcha"/></view>
							<view class="captcha" @click="getCaptcha"><u-image width="100%" height="100rpx" :src="'data:image/png;base64,' + pictureUrl"></u-image></view>
						</view>
					</u-form-item>
				</u-form>
			</view>
			<view class="login">
				<u-button shape="circle" :ripple="true" @click="editPassword" type="success">
					<u-icon name="edit-pen" size="35" class="icon"></u-icon>更新密码
				</u-button>
			</view>
			<br>
			<view class="authorText">
				<view>若您的密码已在官网修改，请在此处更新您的密码</view>
				<view>以便获取最新课程信息。</view>
			</view>
		</view>
	</view>
</template>

<script>
	//import { getCaptcha } from '@/services/user/index.js'
	export default {
		data() {
			return {
				no:'',
				form: {
					no:this.no,
					password: "",
					captcha: ""
				},
				loginButton: {
					marginBottom: '30rpx'
				},
				// rules: {
				// 	no: [{
				// 		required: true,
				// 		message: '请输入账号',
				// 		// 可以单个或者同时写两个触发验证方式 
				// 		trigger: ['change', 'blur'],
				// 	}],
				// 	password: [{
				// 		required: true,
				// 		message: '请输入密码',
				// 		trigger: ['change', 'blur'],
				// 	}],
				// 	captcha: [{
				// 		required: true,
				// 		message: '请输入验证码',
				// 		trigger: ['change', 'blur'],
				// 	}]
				// },
				pictureUrl: '',
				isVerification: false,
				nickName: '',
				avatarUrl: '',
				header: ''
			}
		},
		props: {
			getinfo: {
				type: Function,
				default: null
			}
		},
		async onLoad() {
			let userInfo = uni.getStorageSync("userInfo")
			this.nickName = userInfo.nickName
			this.avatarUrl = userInfo.avatarUrl
			let header = new Object()
			let authority = uni.getStorageSync("Authority")
			header.Authority = authority
			this.header = header
			let data = await getApp().UniRequest("/user/authority/no","GET","",header)
			//console.log(data)
			this.no = data.data.no
		},
		//必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			//this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			async editPassword(){
				let vm = this
				if (vm.form.no == '' || vm.form.password == ''){
					uni.showModal({
						title: '请输入学号或密码',
						showCancel: false
					})
					return
				}
				let body = new Object()
				body.no = vm.no
				body.password = vm.form.password
				uni.showLoading({
					title: '正在修改',
					mask: true
				})
				let data = await getApp().UniRequest("/user/authority","POST",body,vm.header)
				if (data.code == 20003){
					uni.showToast({
						title: '请输入正确的密码',
						icon: 'error'
					})
				}else if (data.code == 20000){
					uni.showToast({
						title: '修改成功!'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		margin: 0 50rpx;

		.logo {
			width: 250rpx;
			height: 400rpx;
			margin: 50rpx auto;
			text-align: center;
			display: flex;
			flex-direction: column;
			align-items: center;

			.logoText {
				position: relative;
				top: 50rpx;
				font-size: 40rpx;
				font-weight: bold;
			}
		}

		.icon {
			margin-right: 20rpx;
		}
		.login {
			margin-top: 30rpx
		}
		.authorText {
			// .authorTitle {
			// 	font-size: 35rpx;
			// 	margin-bottom: 20rpx;
			// }

			// ._li {
			// 	font-size: 25rpx;
			// 	color: #aaa;
			// }
			// margin-bottom: 20rpx;
			text-align: center;
			font-size: 25rpx;
			color: #aaa;
			margin-bottom: 40rpx;
			position: relative;
			top: 50rpx;
		}
		.flex {
			display: flex;
			justify-content: space-between;
			.input {
				width: 200rpx;
			}
			.captcha {
				width: 350rpx;
			}
		}
	}
</style>
